package com.hanrui.android.net.presenter.mine;


import android.content.Context;

import com.hanrui.android.activity.LoginActivity;
import com.hanrui.android.library.base.BaseBean;
import com.hanrui.android.library.base.BasePresenter;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.model.VersionBean;
import com.hanrui.android.library.net.ApiService;
import com.hanrui.android.library.net.RetrofitManager;
import com.hanrui.android.library.net.RxSchedulers;
import com.hanrui.android.net.contract.mine.MineContract;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * 主页
 * Created by menglei on 2018/3/22.
 */

public class MinePresenter extends BasePresenter<MineContract.View> implements MineContract.Presenter {

    @Inject
    public MinePresenter() {
    }

    @Override
    public void getVersion(final Context context) {

        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .getVerion()
                .compose(RxSchedulers.<BaseBean<VersionBean>>applySchedulers())
                .compose(mView.<BaseBean<VersionBean>>bindToLife())
                .subscribe(new Consumer<BaseBean<VersionBean>>() {
                    @Override
                    public void accept(BaseBean<VersionBean> response) throws Exception {
                        if (response.getCode() == 1){
                            mView.getVersion(response.getData());
                        }else if (response.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");

                        }else {
                            mView.showFaild(String.valueOf(response.getMessage()));
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild(throwable.getMessage());
                    }
                });

    }

}
